<template>
  <div class="app-container">
    用户管理
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="用户ID">
        <el-input v-model="userQuery.id" placeholder="用户ID"/>
      </el-form-item>

      <el-form-item label="用户名">
        <el-input v-model="userQuery.username" placeholder="用户名"/>
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>
    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="数据加载中"
      border
      fit
      highlight-current-row>

      <el-table-column label="用户ID" width="70" >
        <template slot-scope="scope">
          <router-link :to="'/user/detail/'+scope.row.id">
            {{ scope.row.id }}
          </router-link>
        </template>
      </el-table-column>

      <el-table-column label="用户名" width="200" >
        <template slot-scope="scope">
          <router-link :to="'/user/detail/'+scope.row.id">
            {{ scope.row.username }}
          </router-link>
        </template>
      </el-table-column>

      <el-table-column label="用户类型" width="150" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.role === 1">
            <el-tag
              type="success"
              effect="dark">
              管理员
            </el-tag>
          </div>
          <div v-else>
            <el-tag
              type=""
              effect="dark">
              普通用户
            </el-tag>
          </div>
        </template>
      </el-table-column>

      <el-table-column prop="email" label="Email" width="250" />

      <el-table-column prop="phone" label="TEL" width="220" />

      <el-table-column label="操作" width="260" align="center">
        <template slot-scope="scope">

          <router-link :to="'/user/detail/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-search">查看</el-button>
          </router-link>

          <router-link :to="'/user/edit/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>
          </router-link>

          <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeDataById(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="current"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="getList"
    />
  </div>
</template>
<script>

import user from '@/api/mall/user'
export default {
  data() {
    return {
      productNames: [],
      listLoading: false,
      list: [],
      current: 1,
      limit: 5,
      total: 10,
      userQuery: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList(current = 1) {
      this.current = current
      user.getUserListPage(this.current, this.limit, this.userQuery)
        .then(response => {
          this.list = response.data.rows
          this.total = response.data.total
        }).catch(response => {
          console.log('error')
        })
    },
    resetData() {
      this.userQuery = {}
      this.getList()
    },
    removeDataById(id) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return user.deleteUser(id)
      }).then(() => {
        this.getList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch((response) => { // 失败
        if (response === 'cancel') {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        } else {
          this.$message({
            type: 'error',
            message: '删除失败'
          })
        }
      })
    }
  }
}
</script>
